<?php

namespace app\admin\controller\admin;

use support\Request;
use app\admin\controller\BaseController;
use app\admin\service\admin\AdminService;
use app\admin\service\admin\RoleService;
use app\admin\service\admin\DepartmentService;

class ManageController extends BaseController
{
    protected array $noNeedLogin = [];

    public function __construct(AdminService $service)
    {
        parent::__construct();
        $this->service = $service;
    }

    public function index(Request $request,RoleService $roleService,DepartmentService $departmentService)
    {
        $do = $request->get('do');
        if ($request->isAjax() && $do == 'json') {
            [$page, $limit, $where] = $this->getPageValue();print_r($where);
            $result = $this->service->loadList($where, $page, $limit);

            if ($result['count'] == 0) {
                $data = [
                    'code' => -1,
                    'msg' => '未查询到数据'
                ];
            } else {
                $data = [
                    'code' => 0,
                    'msg' => '',
                    'count' => $result['count'],
                    'data' => $result['list']
                ];
            }
            return json($data);
        }

        $roleArr = array_column($roleService->getRoleList(), 'name', 'id');

        $this->assign([
            'organ' => json_encode($departmentService->getDeptList()),
            'roles' => json_encode($roleArr),
        ]);

        return $this->fetch('admin/manage/index');
    }

    public function add(Request $request)
    {
        if ($request->isAjax()) {
            $data = $this->checkField(['dept_id', 'role_id', 'username', 'password', 'repassword', 'nickname|null', 'mobile|null']);
            if($data['password'] != $data['repassword']){
                return json(['code' => -1, 'msg' => '两次密码输入不一致']);
            }
            unset($data['repassword']);

            return $this->service->addAddmin($data);
        }
        return json(['code' => -1, 'msg' => '请求错误']);
    }

    public function edit(Request $request)
    {
        if ($request->isAjax()) {
            $data = [];
            $do = $request->get('do', '');
            if ($do == '') {
                $data = $this->checkField(['id', 'dept_id', 'role_id', 'username', 'nickname|null', 'mobile|null']);
            }
            if ($do == 'fast') {
                $fileData = $this->checkField(['id', 'af', 'av|null']);
                $data = [
                    'id' => $fileData['id'],
                    $fileData['af'] => $fileData['av'],
                ];
            }
            if ($do == 'switch') {
                $fileData = $this->checkField(['id', 'af', 'av|1']);
                $data = [
                    'id' => $fileData['id'],
                    $fileData['af'] => $fileData['av'],
                ];
            }

            if (!empty($data)) {
                return $this->service->editAddmin($data);
            }
        }
        return json(['code' => -1, 'msg' => '请求错误']);
    }

    public function del(Request $request)
    {
        if ($request->isAjax()) {
            $data = $this->checkField(['id']);
            return $this->service->delAddmin($data);
        }
        return json(['code' => -1, 'msg' => '请求错误']);
    }

    public function reset(Request $request)
    {
        if ($request->isAjax()) {
            $data = $this->checkField(['id','newPassword','rePassword']);
            if($data['newPassword'] != $data['rePassword']){
                return json(['code' => -1, 'msg' => '两次密码输入不一致']);
            }

            $data['password'] = $data['newPassword'];
            unset($data['newPassword']);
            unset($data['rePassword']);
            return $this->service->resetPwd($data);
        }
        return json(['code' => -1, 'msg' => '请求错误']);
    }

    public function myInfo(Request $request)
    {

    }

}